define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'xilujob/company_profile/index' + location.search,
                    add_url: 'xilujob/company_profile/add',
                    edit_url: 'xilujob/company_profile/edit',
                    del_url: 'xilujob/company_profile/del',
                    multi_url: 'xilujob/company_profile/multi',
                    import_url: 'xilujob/company_profile/import',
                    table: 'xilujob_company_profile',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                //启用固定列
                fixedColumns: true,
                //固定右侧列数
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'user.email', title: __('User.email'), operate: 'like'},
                        {field: 'user.mobile', title:  __('User.mobile'), operate: 'like'},
                        {field: 'enterpriseinfo.company_name', title:  __('Enterpriseinfo.company_name'), operate: 'like'},
                        {field: 'enterpriseinfo.industry_category_id',title: __('Enterpriseinfo.industry_category_id'), searchList: Config.industry_category, formatter: Table.api.formatter.normal},
                        {field: 'top', title: __('Top'), operate: false,sortable:true},
                        {field: 'user.status', title: __('User.status'), searchList: {"normal":__('User.status normal'),"hidden":__('User.status hidden')}, formatter: Table.api.formatter.status},
                        {field: 'audit_status', title: __('Audit_status'), searchList: {0:__('Audit_status 0'),1:__('Audit_status 1'),2:__('Audit_status 2'),3:__('Audit_status 3')},formatter:function(val,row,index){
                                var a = '<a href="'+Fast.api.fixurl('xilujob/company_profile/enterprise_info/ids/'+row.id)+'" class="btn btn-success btn-xs btn-dialog">查看资料</a>';
                                return Table.api.formatter.status.call(this, val,row,index) + a;
                            }},
                        {field: 'personal_audit_status', title: __('Personal_audit_status'), searchList: {0:__('Personal_audit_status 0'),1:__('Personal_audit_status 1'),2:__('Personal_audit_status 2'),3:__('Personal_audit_status 3')},formatter: function(val,row,index){
                                var a = '<a href="'+Fast.api.fixurl('xilujob/company_profile/personal_info/ids/'+row.id)+'" class="btn btn-success btn-xs btn-dialog">查看资料</a>';
                                return Table.api.formatter.status.call(this, val,row,index) + a;
                            }},
                        {field: 'enterpriseinfo.daily_resume_action_num', title: '每日免费下载次数', operate: false},
                        {field: 'enterpriseinfo.resume_action_num', title: '下载剩余次数', operate: false},
                        {field: 'enterpriseinfo.job_num_limit', title: '职位限制总数', operate: false},
                        {field: 'createtime', title: '注册时间', operate: false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate, buttons:
                                [
                                    // {
                                    //     name: 'recommend_resume',
                                    //     title: '推荐人才',
                                    //     text: '推荐人才',
                                    //     // extend: 'data-toggle="tooltip"',
                                    //     classname: 'btn btn-xs btn-primary btn-dialog',
                                    //     url: 'company_recommend_resume/index',
                                    //     icon: ''
                                    // },
                                    // {
                                    //     name: 'login_as',
                                    //     title: '登录',
                                    //     text: '登录',
                                    //     extend: 'data-toggle="tooltip" target="_blank"',
                                    //     classname: 'btn btn-xs btn-primary',
                                    //     url: Fast.api.fixurl('//index.php/index/user/login_as/group_id/2'),
                                    //     icon: ''
                                    // }
                                ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            $('.btn-edit-limit').on('click', function () {
                var ids = Table.api.selectedids(table);
                var field = $(this).data('field');
                Layer.prompt('请输入',function (value,index) {
                    value = parseInt(value);
                    if(!(value>=0)) {
                        Toastr.error('输入0或正整数');
                        return;
                    }
                    Fast.api.ajax({
                        url: 'xilujob/company_profile/multi_info/ids/'+ids.join(','),
                        data: {params:field+'='+value},
                    },function () {
                        table.bootstrapTable('refresh');
                        Layer.close(index);
                    })
                })
            });

        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'xilujob/company_profile/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'xilujob/company_profile/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'xilujob/company_profile/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },

        enterprise_info: function(){
            Controller.api.bindevent();
        },
        personal_info: function(){
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});